<?php

namespace App\Repository\Repositories;


use App\Http\Controllers\web\MemberController;
use App\Http\Service\web\Redisd;
use App\Http\Service\web\Siberian;
use App\Models\MemberEquity;
use App\Models\MemberSiberian;
use App\Models\MemberType;
use App\Models\UserLevel;
use App\Repository\RepositoryInterface\MemberInterface;
use Illuminate\Support\Facades\Redis;

//会员repositories
class MemberRepositories implements MemberInterface
{
    protected $memberEquity;

    public function __construct(MemberEquity $memberEquity)
    {
        $this->memberEquity = $memberEquity;
    }

    public function list()
    {
        echo '123';
    }

    /**
     * 会员类型
     * @return MemberType[]|\Illuminate\Database\Eloquent\Collection
     */
    public function MemberType()
    {
        return MemberType::all();
    }

    /**
     * 会员权益
     * @return mixed
     */
    public function memberEquity()
    {
        return $this->memberEquity->memberEquity();
    }

    /**
     * 卡密会员
     * @return mixed
     */
    public function SiberianMember()
    {
        return MemberSiberian::paginate(10);
    }

    /**
     * 卡密会员添加
     * @param $data
     * @return mixed
     */
    public function memberSiberianAdd($data)
    {
        unset($data['s']);
        return MemberSiberian::create($data);
    }

    /**
     * 循环生成卡密
     * @param $data
     */
    public function SiberianCreate($data)
    {
        $codeArray = [];
        for ($i=1;$i<=$data['number'];$i++){
            $code = Siberian::siberian();
            array_push($codeArray,$code);
        }
        Redisd::getInstance()->zAdd($data['name'],$codeArray);
    }

    /**
     * 卡密会员搜索
     * @param $keyword
     * @return mixed
     */
    public function memberSiberianSearch($keyword){
        return (new MemberSiberian())->memberSiberianSearch($keyword);
    }

    /**
     * 会员类型开启
     * @param $id
     * @return bool|void
     */
    public function memberTypeBegin($id)
    {
        $res = (new MemberType())->memberTypeBegin($id);
        if ($res){
            return true;
        }
    }

    /**
     * 会员记录
     * @return mixed
     */
    public function memberRecord()
    {
        return UserLevel::paginate(5);
    }

    /**
     * 会员类型添加
     * @param $data
     * @return bool|void
     */
    public function memberTypeAdd($data)
    {
        $res = (new MemberType())->memberTypeAdd($data);
        if ($res){
            return true;
        }
    }

    /**
     * 会员类型删除
     * @param $id
     * @return bool|void
     */
    public function memberTypeDel($id)
    {
        $res = (new MemberType())->memberTypeDel($id);
        if ($res){
            return true;
        }
    }

}
